<?php
/**
 * modelfield
 * 模型字段
 * @author Administrator
 * @version
 */
class Model_Tables_Modelfield extends Zend_Db_Table_Abstract {
    /**
     * The default table name
     */
    protected $_name = 'modelfield';
    protected $_modelfields = array('text'=>'单行文本',
				'textarea'=>'多行文本',
				'editor'=>'编辑器',
    			'hidden'=>'隐藏表单',
    			'datetime'=>'日期时间',
				'checkbox'=>'选项',
				'select'=>'下拉列表',
				'radio'=>'单选框',
    			'subtable'=>'子表'
				);
	protected $_modelfieldcreate = array('text'=>"varchar (20) DEFAULT '' NOT NULL",
				'textarea'=>"varchar (255) DEFAULT '' NOT NULL",
				'editor'=>"mediumtext DEFAULT '' NOT NULL",
				'hidden'=>"varchar (255) DEFAULT '' NOT NULL",
			    'datetime'=>"varchar (255) DEFAULT '' NOT NULL",
				'checkbox'=>"varchar (255) DEFAULT '' NOT NULL",
				'select'=>"varchar (255) DEFAULT '0' NOT NULL",
				'radio'=>"varchar (255) DEFAULT '0' NOT NULL",
				'subtable'=>"varchar (10) DEFAULT '' NOT NULL"
				);
    
    public function add( $params ,$t)
    {
    	$db = $this->getAdapter();  
    	$data = array(
		'modelid' => $params['modelid'],
		'field' => $params['field'],
		'name' => $params['name'],
		'tips' => $params['tips'],
		'css' => $params['css'],
		'minlength' => $params['minlength'],
		'maxlength' => $params['maxlength'],
		'pattern' => $params['pattern'],
		'errortips' => $params['errortips'],
		'formtype' => $params['formtype'],
		'issystem' => $params['issystem'],
		'issearch' => $params['issearch'],
		'isselect' => $params['isselect'],
		'islist' => $params['islist'],
		'isshow' => $params['isshow'],
		'isfulltext' => $params['isfulltext'],
		'listorder' => $params['listorder'],
		'disabled' => $params['disabled'],
    	'defaultvalue' => $params['defaultvalue'],
        );
		$createsql = "alter table `$t` add column `".$params['field']."` ".$this->_modelfieldcreate[$params['formtype']];
		$insertsql = $db->quoteInto("insert into modelfield( modelid, field, name, tips, css, minlength,maxlength, pattern, errortips, formtype, issystem, issearch, isselect, islist, isshow, isfulltext,	listorder,disabled,defaultvalue) values (?)",$data);
		$db->beginTransaction();
		try {
			$db->query($createsql);
			$db->query($insertsql);
			$db->commit();
		} catch (Exception $e) {
		    $db->rollBack();
		    return false;
		}
		return true;
    }
    public function modify( $params, $fieldid )
    {
        $data = array(
		//'modelid' => $params['modelid'],
		'field' => $params['field'],
		'name' => $params['name'],
		'tips' => $params['tips'],
		'css' => $params['css'],
		'minlength' => $params['minlength'],
		'maxlength' => $params['maxlength'],
		'pattern' => $params['pattern'],
		'errortips' => $params['errortips'],
		'formtype' => $params['formtype'],
		'issystem' => $params['issystem'],
		'issearch' => $params['issearch'],
		'isselect' => $params['isselect'],
		'islist' => $params['islist'],
		'isshow' => $params['isshow'],
		'isfulltext' => $params['isfulltext'],
		'listorder' => $params['listorder'],
		'disabled' => $params['disabled'],
        'defaultvalue' => $params['defaultvalue'],
        );
        $where = 'fieldid = '.$fieldid ;
        
        return $this->update($data , $where );  
              
    }
    public function modifyAjax( $fieldid, $field, $value )
    {
    	$data = array($field => $value);
        $where = 'fieldid = '.$fieldid;
        return $this->update($data , $where );
    }
    public function del( $fieldid,$t,$field )
    {
        $db = $this->getAdapter();
    	$db->beginTransaction();
		try {
			echo "alter table `$t` drop `$field`";
		    $db->query("alter table `$t` drop `$field`");
		    $db->query("delete from `modelfield` where fieldid = '$fieldid'");
		   
		    $db->commit();
		} catch (Exception $e) {
		    $db->rollBack();
		    print_r($e);
		    return false;
		}
		return true;
    }
    public function getInfo($fieldid)
    {
        $row = $this->fetchRow('fieldid = ' . $fieldid);
        if (!$row) {
        	return null;
        }
        else
        {
        	return $row->toArray();
        }
    }
    public function getCount($s = "",$modelid=0)
    { 
        $db = $this->getAdapter();
    	$select = $this->select()->from($this->_name,'count(*) as num');
    	$where = $db->quoteInto("modelid = ?", $modelid);
        if($s!="")  
    	{
			$where .= " and ".$db->quoteInto("field like ?", "%$s%");
    	}
    	$select = $select->where($where);
        $result = $this->fetchRow($select)->num;
        return $result;
    }
    public function getList($page, $numPerPage = 10,$s= "",$modelid=0)
    {
    	$db = $this->getAdapter();
    	$page = (1>$page) ? 1 : $page;
    	$offset = ($page-1)*$numPerPage;
        $orderby = array('listorder','fieldid');
        $where = $db->quoteInto("modelid = ?", $modelid);
        if($s!="")     
		{
			$where .= " and ".$db->quoteInto("field like ?", "%$s%");
		}  
        $result = $this->fetchAll($where,$orderby, $numPerPage, $offset )->toArray();
        return $result;
    }
    public function getModelFieldType()
    {
    	return $this->_modelfields;
    }
    /**
     * modifyAjaxRevers
     * 无刷新字段反转aaa
     * @param $field 需要反转的字段
     * @param $userid 主键值
     */
    public function modifyAjaxRevers( $field ,$pri)
    {
    	$db = $this->getAdapter();
    	$sql = "update modelfield set  $field =not  $field  where fieldid=' $pri'";
		return $db->query($sql);
    }
    
}
 
